<?php

/**
 * uniBoard New Style
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_style{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Style API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Style API based on main class
		$this -> api = new api_style( $appCore, 1);

		// Reset data
		$this -> api -> style_id = 0;
		
		$this -> api -> setStyleName( $appCore -> _POST( 'new_style_name'));
		$this -> api -> setStyleParent( 1);
		$this -> api -> setImagesetId( 1);
		$this -> api -> setStyleCss( $this -> api -> style_css);
		$this -> api -> setStyleJs( $this -> api -> style_js);
		
		// Update style?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Check Error
			if ( $this -> api -> error)
			{
				// Draw error
				$appCore -> output -> error( $this -> api -> last_error, $appCore -> lang['new_style_form']);
				
				// Repeat form
				$this -> drawForm();
			}
			else
			{
				// Save api
				$this -> api -> saveData();
				
				// Create CSS and JS files
				file_put_contents( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id . '.css', class_cache::makeFileStamp( 'STYLE ' . $this -> api -> style_id . ' CSS FILE', false) . str_ireplace( array( '{IMAGES_DIR}', '{BLUEPRINT_DIR}'), array( 'imageset_' . $this -> api -> imageset_id, CSS_FRAMEWORK_PATH), $this -> api -> style_css));
				file_put_contents( STYLES_DATA_PATH . 'jscript_' . $this -> api -> style_id . '.js', class_cache::makeFileStamp( 'STYLE ' . $this -> api -> style_id . ' JAVASCRIPT FILE', false) . $this -> api -> style_js);
				
				// Run resynchroniser
				$this -> resynchStyle( $this -> api -> style_id);
				
				// Redirect us back to styles list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=styles'), $appCore -> lang['acp_style_new_created']);
			}
		}
		else
		{
			// Draw form
			$this -> drawForm();
		}
			
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// New style form start
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_style', true));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['new_style_form_name'], $this -> appCore -> drawTextInput( 'new_style_name', $this -> api -> style_name), $this -> appCore -> lang['new_style_form_name_expl']);
		
		// New style form close
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_style_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_style_form'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Performs compilation of style templates
	 *
	 * @param integer $style_id
	 */
	
	function resynchStyle( $style_id){
		
		// If style directory doesnt exists, create it
		if ( !file_exists( STYLES_DATA_PATH . 'style_' . $style_id . '/index.html'))
		{
			class_files::newDir( STYLES_DATA_PATH . 'style_' . $style_id);
		}
		
		// Get style parents
		$style_parents = $this -> appCore -> getStyleParents( $style_id);
		
		// Select Templates Groups from DB
		$this -> appCore -> db -> query( 'SELECT DISTINCT template_group
		FROM ' . DB_PREFIX . 'styles_templates
		WHERE style_id IN (' . join( ',', $style_parents) . ')');
		
		// Compile Template Groups
		while ( $group_result = $this -> appCore -> db -> fetch_array())
		{
			// Create Compiler
			$tpl_compiler = new class_template_compiler( $this -> appCore, $style_id, $style_parents, $group_result[0]);
			
			// Compile template to specified location
			$tpl_compiler -> compile( STYLES_DATA_PATH . 'style_' . $style_id . '/tpl_' . $this -> appCore -> secureFileName( $group_result[0]). PHP_EXT);	
		}
		
	}
	
}